import BuffUtil from '@/BuffUtil';
import SelectCommon from 'code/Core/Common/SelectCommon';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';
const desc = `每次攻击叠加${ColorSafe}2%|r的闪避/暴击机率，当闪避/暴击成功后失效。|n觉醒效果：当闪避/暴击成功后，使周围${ColorSafe}700|r范围内的友军在随后的5秒内造成伤害/伤害减免提升${ColorSafe}10%`;
const art = 'ReplaceableTextures\\CommandButtons\\BTNDrunkenDodge.blp';
const name = '醉拳';
const config: TalentConfigModel = {
    id: 'talent_t_TalentDrunkenFirst',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_TalentDrunkenFirst',
        art,
        name,
        ubertip: desc,
    },
    buffTypePlus: {
        id: 'buff_t_TalentDrunkenFirst_plus',
        art,
        name: '醉拳[觉醒]',
        ubertip: `每次攻击叠加${ColorSafe}2%|r的闪避/暴击机率，当闪避/暴击成功后失效。当闪避/暴击成功后，使周围${ColorSafe}700|r范围内的友军在随后的5秒内造成伤害/伤害减免提升${ColorSafe}10%`,
    },
};

const common = { name, art, stackCountMax: -1 };

const critBuffType: AppBuffType = {
    ...common,
    id: 'buff_t_TlantDunkenFirst_crit',
    ubertip: '增加你的暴击机率',
    attribute: {
        物理暴击机率: 0.02,
    },
};

const doggeBuffType: AppBuffType = {
    ...common,
    id: 'buff_t_TlantDunkenFirst_dogge',
    ubertip: '增加你的闪避机率',
    attribute: {
        基础闪避: 0.02,
    },
};

const damageBuffType: AppBuffType = {
    ...common,
    id: 'buff_t_TlantDunkenFirst_damage',
    ubertip: '你造成的伤害增加了',
    dur: 5,
    stackDurMode: true,
    attribute: {
        物理伤害倍率: 0.1,
        法术伤害倍率: 0.1,
    },
};

const defBuffType: AppBuffType = {
    ...common,
    id: 'buff_t_TlantDunkenFirst_def',
    ubertip: '你承受的伤害减少了',
    dur: 5,
    stackDurMode: true,
    attribute: {
        物理伤害减免: 0.1,
        法术伤害减免: 0.1,
    },
};

export default class TalentDrunkenFirst extends TalentBase {
    static instance: TalentDrunkenFirst;
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentDrunkenFirst.instance = this;
        this.setTalentConfig(config);
        this.init();
        EventManager.onTakeAttack(this.attackEffect);
        EventManager.闪避攻击(this.doggeEffect);
        EventManager.受到暴击(this.critEffect);
    }
    critEffect(d: DamageEventData) {
        if (!d.isAttach && !d.isAttack) return;
        let _this = TalentDrunkenFirst.instance;
        let { unEffect, plus } = _this.talentUnEffect(d.source);
        if (!plus) return;
        let us = SelectCommon.getAllyUnitInRange(d.source, 700);
        for (let u of us) {
            BuffUtil.addBuff(u, damageBuffType.id, d.source);
        }
    }
    doggeEffect(d: DamageEventData) {
        let buff = BuffUtil.getUnitBuff(d.target, doggeBuffType.id);
        buff != null && buff.destroy();
        let _this = TalentDrunkenFirst.instance;
        let { unEffect, plus } = _this.talentUnEffect(d.target);
        if (plus) {
            let us = SelectCommon.getAllyUnitInRange(d.target, 700);
            for (let u of us) {
                BuffUtil.addBuff(u, defBuffType.id, d.target);
            }
        }
    }
    attackEffect(d: DamageEventData) {
        if (d.hasDogged) return;
        let _this = TalentDrunkenFirst.instance;
        let { unEffect, plus } = _this.talentUnEffect(d.source);
        if (unEffect) return;
        // 命中时增加暴击与闪避
        BuffUtil.addBuff(d.source, critBuffType.id);
        BuffUtil.addBuff(d.source, doggeBuffType.id);
    }
    init() {
        BuffUtil.registerBuffType(critBuffType);
        BuffUtil.registerBuffType(doggeBuffType);
        BuffUtil.registerBuffType(damageBuffType);
        BuffUtil.registerBuffType(defBuffType);
    }
}
